package me.shurufa.activities;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.shurufa.R;
import me.shurufa.db.PersistenceUtils;
import me.shurufa.utils.Global;

/**
 * 闪屏界面
 */
public class SplashActivity extends Activity {

    @Bind(R.id.ll_overlay)
    LinearLayout ll_overlay;
    @Bind(R.id.iv_bg)
    ImageView iv_bg;
    @Bind(R.id.ll_logo)
    LinearLayout ll_logo;

    private static final int TIME_FIRST = 1000;
    private static final int TIME_TOTAL = 5000;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (0 == msg.what) {
                startAnimation();
            } else if (1 == msg.what) {
                startAnimationForSmartisan(); // 坚果动画卡顿 专门适配
            }
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        Global.appInit(this);

        getDeviceInfo(this);

        // 判断坚果
        if (isSmartisanJianGuo()) {
            ll_overlay.setVisibility(View.GONE);
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mHandler.sendEmptyMessage(1);
                }
            }, TIME_FIRST);
            return;
        }

        // 文字淡出
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1.0f);
        alphaAnimation.setDuration(800);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        ll_logo.startAnimation(alphaAnimation);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(0);
            }
        }, TIME_FIRST);
    }

    private void startAnimationForSmartisan() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (PersistenceUtils.isLogin()) {// Global.isLoggedIn()) {
                    startActivity(new Intent(SplashActivity.this, NewMainActivity.class));
                    finish();
                    System.gc();
                } else {
                    startActivity(new Intent(SplashActivity.this, NewLoginActivity.class));
                    finish();
                    System.gc();
                }
            }
        }, 3000);
    }

    /**
     * 背景动画
     */
    private void startAnimation() {
        // 淡出
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0f);
        alphaAnimation.setDuration(TIME_TOTAL - TIME_FIRST);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (PersistenceUtils.isLogin()) {// Global.isLoggedIn()) {
                    startActivity(new Intent(SplashActivity.this, NewMainActivity.class));
                    finish();
                    System.gc();
                } else {
                    startActivity(new Intent(SplashActivity.this, NewLoginActivity.class));
                    finish();
                    System.gc();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        ll_overlay.startAnimation(alphaAnimation);

        // 图片缩放
        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.15f, 1.0f, 1.15f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(TIME_TOTAL - TIME_FIRST);
        scaleAnimation.setFillAfter(true);
        iv_bg.startAnimation(scaleAnimation);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    public boolean isSmartisanJianGuo() {
        return "YQ601".equalsIgnoreCase(Build.MODEL)
                || "YQ603".equalsIgnoreCase(Build.MODEL)
                || "YQ605".equalsIgnoreCase(Build.MODEL)
                || "YQ607".equalsIgnoreCase(Build.MODEL);
    }

    @SuppressLint("NewApi")
    public static boolean checkPermission(Context context, String permission) {
        boolean result = false;
        if (Build.VERSION.SDK_INT >= 23) {
            if (context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
                result = true;
            }
        } else {
            PackageManager pm = context.getPackageManager();
            if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
                result = true;
            }
        }
        return result;
    }

    public static String getDeviceInfo(Context context) {
        try {
            org.json.JSONObject json = new org.json.JSONObject();
            android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String device_id = null;
            if (checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
                device_id = tm.getDeviceId();
            }
            android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            String mac = wifi.getConnectionInfo().getMacAddress();
            json.put("mac", mac);
            if (TextUtils.isEmpty(device_id)) {
                device_id = mac;
            }
            if (TextUtils.isEmpty(device_id)) {
                device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
                        android.provider.Settings.Secure.ANDROID_ID);
            }
            json.put("device_id", device_id);
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
